package com.lijian.community.controller;

import com.lijian.community.dto.NoticeDTO;
import com.lijian.community.dto.PaginationDTO;
import com.lijian.community.mapper.UserMapper;
import com.lijian.community.model.User;
import com.lijian.community.service.NoticeService;
import com.lijian.community.service.QuestionService;
import com.mysql.cj.protocol.x.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

@Controller
public class ProfileController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/profile/{action}")
    public String profile(@PathVariable("action") String action,
                          Model model,
                          HttpServletRequest request,
                          @RequestParam(name = "page", defaultValue = "1") Integer page,
                          @RequestParam(name = "size", defaultValue = "5") Integer size) {

        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/";
        }
        if (action.equals("questions")) {
            model.addAttribute("section", "questions");
            model.addAttribute("sectionName", "我的提问");
            PaginationDTO pagination = questionService.findAll(user.getId(), page, size);
            model.addAttribute("pagination", pagination);
        } else if (action.equals("replies")) {
            PaginationDTO pagination = noticeService.findAll(user.getId(), page, size);
            //通知计数做到了session里
            //Integer unreadCount = noticeService.unreadCount(user.getId());
            model.addAttribute("section", "replies");
            model.addAttribute("pagination", pagination);
            //model.addAttribute("unreadCount", unreadCount);
            model.addAttribute("sectionName", "最新回复");

        }
        return "profile";
    }

}
